package IO流;

import org.junit.Test;

import java.io.*;

/*
    缓冲流
         为了提高数据读写的速度,Java API提供了带缓冲功能的流类,在使用这些流类时,会创建一个内部缓冲区数组,缺省使用8192个字节(8Kb)的缓冲区
         缓冲流要"套接"在相应的节点流之上,根据数据操作单位可以把缓冲流分为:
           BufferedInputStream 和 BufferedOutputStream
           BufferedReader 和 BufferedWriter
         当读取数据时,数据按块读入缓冲区,其后的读操作则直接访问缓冲区
         当使用BufferedInputStream读取字节文件时,BufferedInputStream会一次性从文件中读取8192个(8Kb),存在缓冲区中,直到缓冲区装满了,才重新从文件中读取下一个8192个字节数组
         向流中写入字节时,不会直接写到文件,先写到缓冲区中直到缓冲区写满,BufferedOutputStream才会把缓冲区中的数据一次性写到文件里,使用方法 flush()可以强制将缓冲区的内容全部写入输出流
         关闭流的顺序和打开流的顺序相反,只要关闭最外层流即可,关闭最外层流也会相应关闭内层节点流
         flush()方法的使用:手动将buffer中内容写入文件
         如果是带缓冲区的流对象的close()方法:不但会关闭流,还会在关闭流之前刷新缓冲区,关闭后不能再写出

 */
public class IO03 {

    //实现非文本文件的复制
    @Test
    public void test1(){

        long start = System.currentTimeMillis();

        File src = new File("E:\\FileTest\\test1.avi");
        File desc = new File("E:\\FileTest\\test2.avi");

        InputStream input = null;
        OutputStream output = null;

        try {
            input = new BufferedInputStream(new FileInputStream(src));  //加入缓冲流
            output = new BufferedOutputStream(new FileOutputStream(desc)); //加入缓冲流

            byte[] array = new byte[1024];
            int len;

            while((len = input.read(array)) != -1){
                output.write(array,0,len);
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(output != null){
                try {
                    output.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if(input != null){
                try {
                    input.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        long end = System.currentTimeMillis();
        System.out.println(end - start);
    }

    //实现文本文件的复制
    @Test
    public void test2(){
        long start = System.currentTimeMillis();

        File src = new File("E:\\FileTest\\Writer.txt");
        File desc = new File("E:\\FileTest\\Writer1.txt");

        Reader reader = null;
        Writer writer = null;

        try {
            reader = new BufferedReader(new FileReader(src));
            writer = new BufferedWriter(new FileWriter(desc));

            char[] array = new char[1024];
            int len;

            while((len = reader.read(array)) != -1){
                writer.write(array,0,len);
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(writer != null){
                try {
                    writer.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if(reader != null){
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        long end = System.currentTimeMillis();
        System.out.println(end - start);
    }

    //实现文本文件的复制
    @Test
    public void test3(){
        long start = System.currentTimeMillis();

        File src = new File("E:\\FileTest\\Writer.txt");
        File desc = new File("E:\\FileTest\\Writer1.txt");

        BufferedReader reader = null;
        BufferedWriter writer = null;

        try {
            reader = new BufferedReader(new FileReader(src));
            writer = new BufferedWriter(new FileWriter(desc));

            String str;
            while((str = reader.readLine()) != null){
                writer.write(str); //读取一行时不包含换行符,需要手动添加,否则复制完成后只有一行
                writer.newLine(); //换行
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(writer != null){
                try {
                    writer.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if(reader != null){
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        long end = System.currentTimeMillis();
        System.out.println(end - start);
    }
}
